﻿using NFinalCompiler.SqlModel;
using System.Linq;
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Threading.Tasks;
using NFinal;

//此代码由NFinalCompiler生成。
//http://bbs.nfinal.com
namespace NFinalCompiler.SqlModel.NetStandard
{
    /// <summary>
    /// 模板类
    /// </summary>
    [View("/NFinalCompiler/SqlModel/NetStandard/DAL.cshtml")]
    public class DALRazor : NFinal.View.RazorView<ModelData>
    {
        /// <summary>
        /// 模板类初始化函数
        /// </summary>
        /// <param name="writer">写对象</param>
        /// <param name="Model">数据</param>
        public DALRazor(NFinal.IO.Writer writer, ModelData Model) : base(writer, Model)
        {
        }
        /// <summary>
        /// 输出模板内容
        /// </summary>
        public override void Execute()
        {
            this.ExecuteAsync().Wait();
        }
        /// <summary>
        /// 输出模板内容(异步)
        /// </summary>
        public override async Task ExecuteAsync()
        {
            await writer.WriteAsync("");
            await writer.WriteAsync("");
            await writer.WriteAsync("using ");
            await writer.WriteAsync(Model.ModelNameSpace);
            await writer.WriteAsync(".Base.BaseDAL;\r\nnamespace  ");
            await writer.WriteAsync(Model.ModelNameSpace);
            await writer.WriteAsync(".ModelFiter.");
            await writer.WriteAsync(Model.DataBaseShortName);
            await writer.WriteAsync("\r\n{\r\n    /// <summary>\r\n    /// ");
            await writer.WriteAsync(Model.Table.Description);
            await writer.WriteAsync("DAL\r\n    ///</summary>\r\n    public class ");
            await writer.WriteAsync(ModelHelper.GetTableName(Model.Table));
            await writer.WriteAsync("Dal : BaseDAL\r\n    {\r\n        public ");
            await writer.WriteAsync(ModelHelper.GetTableName(Model.Table));
            await writer.WriteAsync("Dal()\r\n        {\r\n            _Field = \"");
            await writer.WriteAsync(ModelHelper.GetCol(Model.Table));
            await writer.WriteAsync("\";\r\n            _Primary = \"");
            await writer.WriteAsync(Model.Table.PrimaryKey == null ? null : Model.Table.PrimaryKey.Columns.First());
            await writer.WriteAsync("\";\r\n            _FieldAt = \"");
            await writer.WriteAsync(ModelHelper.GetColp(Model.Table));
            await writer.WriteAsync("\";\r\n            _Table = \"");
            await writer.WriteAsync(Model.Table.Name);
            await writer.WriteAsync("\";\r\n            _FieldUp = \"");
            await writer.WriteAsync(ModelHelper.GetColu(Model.Table));
            await writer.WriteAsync("\";\r\n            _IsIdentity = ");
            await writer.WriteAsync(Model.Table.HasAutoNumberColumn);
            await writer.WriteAsync(";//主建是否自增，如果不自增插入的时候要赋值\r\n            _DataBase= \"");
            await writer.WriteAsync(Model.DataBaseShortName);
            await writer.WriteAsync("\";\r\n        }\r\n    }\r\n}");
        }
    }
}
